package org.example.struct.array2;

import java.util.*;

/**
 * @author liyishan
 * @date 2023/10/30 10:40
 * @apiNote   本质上是说能否把一个数组分成两个数组，使得每对元素中一个数是另一个数的两倍
 */

public class Test00954CanReorderDoubled {

    public static void main(String[] args) {
        int[] arr ={4,-2,2,-4};
        boolean res = test(arr);
        System.out.println(res);
    }
    public static boolean test(int[] arr) {
        Map<Integer,Integer> cnt = new HashMap<>();
        for(int x:arr){
            cnt.put(x,cnt.getOrDefault(x,0)+1);
        }
        if(cnt.getOrDefault(0,0)%2!=0){
            return false;
        }
        List<Integer> vals = new ArrayList<>();
        for(int x :cnt.keySet()){
            vals.add(x);
        }
        Collections.sort(vals, Comparator.comparingInt(Math::abs));
        for(int x:vals){
            if(cnt.getOrDefault(2*x,0) < cnt.get(x)){
                return false;
            }
            cnt.put(2*x,cnt.getOrDefault(2*x,0)-cnt.get(x));
        }
        return true;
    }
}
